﻿/*
	Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef CheckBox_h__
#define CheckBox_h__

#include "AbstractButton.h"
#include "../utility/SmartPtrDef.h"
#include "../base/Rect.h"

namespace SkyEngine2d
{
	
	class Text;
	/*!
	 \brief 选择框
	*/
	class SGE_API CheckBox
		:public AbstractButton
	{
		UI_CLASS(CheckBox)
	
	public:
	
		//! Checkout 的选中状态 
		enum class CheckState
		{
			Unchecked, /*!< 未选中. */
			Checked	 /*!< 选中. */
		};
	
	public:
	
		//!	创建一个CheckBox  
		/*!
		\param const WidgetSharedPtr & parent  父控件
		\return CheckBoxSharedPtr
		*/
		static CheckBox* create(Widget* parent = nullptr);
	
		//! 设置文本的渲染区域 
		/*!
		\param const Rect & rect 相对于checkbox 控件的区域
		\return void  
		*/
		void setTextRect(const Rect& rect);
	
		//! 设置 选中时 填充的图片
		/*!
		\param std::unique_ptr<Scale9Image> image 填充的图片 
		\param Rect & rect 图片的绘制区域 ，相对于控件
		\return void  
		*/
		void setCheckedFillImage(std::unique_ptr<Scale9Image> image,const Rect& rect);
	
		//! 设置选中时填充的区域
		/*!
		\param const Rect & rect  图片的绘制区域 ，相对于控件
		\return void  
		*/
		void setCheckedFillImageRect(const Rect & rect);
	
		void setChangeStateListener(const std::function<void(CheckState curr_state)>& listener);
	
		void setCheckState(CheckState state);
	
		CheckState getCheckState()const;
	protected:
		CheckBox(Widget* parent);
	
		virtual bool init() override;
	
		virtual void onMouseReleaseEvent(EventMouse* event) override;
	
		virtual void onMoveEvent(EventWidgetMove* e) override;
	
		virtual void onResizeEvent(EventResize* e) override;
	
		virtual void draw(IRender* render, const Matrix4X4& transform) override;
	
	private:
		virtual int getDefultWidth() const override;
		virtual int getDefultHeight() const override;
	private:
		std::unique_ptr<Scale9Image> m_fillImage;
		Rect       m_fillRect;
		Rect       m_fillRenderRect;
		CheckState m_checkState;   /*!< 当前的选中状态. */
		Rect	   m_textRenderRect;  /*!< 最终绘制文本的区域. */
		Rect	   m_textRect;    /*!< 绘制文字的区域. */
		using Listener = std::function<void(CheckState curr_state)>;
		Listener	m_checkStateListener; /*!< 监听状态的变化. */
	};
	
}


#endif // CheckBox_h__
